<template>
  <div class="flex flex-row w-full justify-between">
    <div class="px-[10px] video-container">
      <div class="mb-4">
        <div class="mb-2">标题</div>
        <el-input
          v-model="videoStore.videoInfo.title"
          style="width: 100%"
          placeholder="请输入标题"
        />
      </div>
      <div class="mb-4">
        <div class="mb-2">视频类型</div>
        <el-select
          v-model="videoStore.videoInfo.type"
          placeholder="Select"
          style="width: 240px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="mb-4">
        <div class="mb-2">封面</div>
        <el-upload
          class="avatar-uploader"
          action="http://101.34.248.54:8092/api/wuli-file/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="imageUrl"
            :src="imageUrl"
            class="avatar"
          />
          <el-icon
            v-else
            class="avatar-uploader-icon"
          >
            <Plus />
          </el-icon>
        </el-upload>
      </div>
      <div class="mb-8">
        <div class="mb-2">上传视频</div>
        <el-upload
          class="upload-demo"
          drag
          action="http://101.34.248.54:8092/api/wuli-file/upload"
          multiple
          :on-success="handleVideoSuccess"
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">
            Drop file here or <em>click to upload</em>
          </div>
        </el-upload>
      </div>
      <div class="mb-4 w-full flex justify-center">
        <el-button
          type="primary"
          class="w-[500px]"
          @click="addNewVideo"
        >保存</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Plus, UploadFilled } from '@element-plus/icons-vue'
import { ref } from 'vue'
import service from '../../service/service'
import type { UploadProps } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useVideoStore } from '../../stores/video'
const videoStore = useVideoStore()
import { useUserStore } from '../../stores/user'
const userStore = useUserStore()
const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (response) => {
  imageUrl.value =
    'http://101.34.248.54:8092/api/wuli-file/download?filename=' + response.data
}

const videoUrl = ref('')
const handleVideoSuccess: UploadProps['onSuccess'] = (response) => {
  videoUrl.value =
    'http://101.34.248.54:8092/api/wuli-file/download?filename=' + response.data
  console.log(videoUrl.value)
}

const addNewVideo = () => {
  videoStore.videoInfo.author = userStore.userinfo
  videoStore.videoInfo.cover = imageUrl.value
  videoStore.videoInfo.path = videoUrl.value
  service({
    method: 'post',
    url: '/wuli-video/save',
    data: { data: videoStore.videoInfo },
  }).then((result: any) => {
    if (result.data.code === 200) {
      imageUrl.value = ''
      videoStore.videoInfo.title = ''
      ElMessage({
        message: '发布成功',
        type: 'success',
      })
    } else {
      ElMessage({
        message: '发布失败',
        type: 'error',
      })
    }
  })
}

const options = [
  {
    value: '工学',
    label: '工学',
  },
  {
    value: '理学',
    label: '理学',
  },
  {
    value: '历史学',
    label: '历史学',
  },
  {
    value: '会计学',
    label: '会计学',
  },
  {
    value: '经济学',
    label: '经济学',
  },
  {
    value: '艺术学',
    label: '艺术学',
  },
  {
    value: '教育学',
    label: '教育学',
  },
  {
    value: '哲学',
    label: '哲学',
  },
  {
    value: '文学',
    label: '文学',
  },
  {
    value: '法学',
    label: '法学',
  },
  {
    value: '管理学',
    label: '管理学',
  },
  {
    value: '医学',
    label: '医学',
  },
  {
    value: '农学',
    label: '农学',
  },
]
</script>


<style scoped>
.video-container {
  margin-top: 10px;
  width: 100%;
  padding: 20px;
  background-color: white;
  box-shadow: 12.5px 12.5px 10px rgba(0, 0, 0, 0.024),
    100px 100px 80px rgba(0, 0, 0, 0.07);
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>